//
//  NRWCarUser.m
//  New_Runway
//
//  Created by mack on 2018/1/23.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "NRWCarUser.h"
//#import "NRWCarTabbarController.h"
//#import "NRWCarNavigationController.h"
#define userPath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"User"]

@implementation NRWCarUser
- (void)setWithDictionary:(NSDictionary *)dic
{
    
    [self setValuesForKeysWithDictionary:dic];
    
}
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
    
}
+(instancetype)sharedUser
{
    __strong static NRWCarUser *shareManager;
    
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        
        if ([[NSFileManager defaultManager] fileExistsAtPath:userPath]) {//解档
            
            shareManager = [NSKeyedUnarchiver unarchiveObjectWithFile:userPath];
            
        } else {
            
            shareManager = [[[self class] alloc] initPrivate];
        }
        
    });
    return shareManager;
}

- (id)initPrivate
{
    self = [super init];
    if (self) {
        _didLogin = NO;
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginSuccessed) name:@"kLoginSuccess" object:nil];
        
    }
    return self;
}

-(void)loginSuccessed{
    
    
}
-(void)archive
{
    //NSString * path = [[self class]archivePath];
    
    BOOL result = [NSKeyedArchiver archiveRootObject:self toFile:userPath];
    
    
    if (result) {
        NSLog(@"archive %@ successfully!", NSStringFromClass([self class]));
        
    } else {
        
        NSLog(@"failed to archive %@!", NSStringFromClass([self class]));
    }
    
    
}

//归档
- (void)encodeWithCoder:(NSCoder *)aCoder
{
    
    [aCoder encodeBool:self.didLogin forKey:@"didLogin"];
    [aCoder encodeObject:self.accountId forKey:@"accountId"];
    [aCoder encodeObject:self.accountId forKey:@"accountName"];
    [aCoder encodeObject:self.accountPropertyMap forKey:@"accountPropertyMap"];
    [aCoder encodeObject:self.applicationId forKey:@"applicationId"];
    [aCoder encodeObject:self.marketId forKey:@"marketId"];
    [aCoder encodeObject:self.marketLogo forKey:@"marketLogo"];
    [aCoder encodeObject:self.marketName forKey:@"marketName"];
    [aCoder encodeObject:self.resourceList forKey:@"resourceList"];
    [aCoder encodeObject:self.roleList forKey:@"roleList"];
    [aCoder encodeObject:self.shopId forKey:@"shopId"];
    [aCoder encodeObject:self.shopName forKey:@"shopName"];
    [aCoder encodeObject:self.resourceUrl forKey:@"resourceUrl"];
    [aCoder encodeObject:self.userId forKey:@"userId"];
    [aCoder encodeObject:self.userMobile forKey:@"userMobile"];
    [aCoder encodeObject:self.userName forKey:@"userName"];
    
    [aCoder encodeObject:self.password forKey:@"password"];
    [aCoder encodeObject:self.phoneNum forKey:@"phoneNum"];
    
    
}

//解档
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder
{
    
    self.didLogin = [aDecoder decodeBoolForKey:@"didLogin"];
    self.accountId = [aDecoder decodeObjectForKey:@"accountId"];
    self.accountName = [aDecoder decodeObjectForKey:@"accountName"];
    self.accountPropertyMap = [aDecoder decodeObjectForKey:@"accountPropertyMap"];
    self.applicationId = [aDecoder decodeObjectForKey:@"applicationId"];
    self.marketId = [aDecoder decodeObjectForKey:@"marketId"];
    self.marketLogo = [aDecoder decodeObjectForKey:@"marketLogo"];
    self.marketName = [aDecoder decodeObjectForKey:@"marketName"];
    self.resourceList = [aDecoder decodeObjectForKey:@"resourceList"];
    self.roleList = [aDecoder decodeObjectForKey:@"roleList"];
    self.shopId = [aDecoder decodeObjectForKey:@"shopId"];
    self.shopName = [aDecoder decodeObjectForKey:@"shopName"];
    self.resourceUrl = [aDecoder decodeObjectForKey:@"resourceUrl"];
    self.userId = [aDecoder decodeObjectForKey:@"userId"];
    self.userMobile = [aDecoder decodeObjectForKey:@"userMobile"];
    self.userName = [aDecoder decodeObjectForKey:@"userName"];
    
    self.password = [aDecoder decodeObjectForKey:@"password"];
    self.phoneNum = [aDecoder decodeObjectForKey:@"phoneNum"];

    
    return self;
}

+ (NSString *)archivePath
{
    NSString * path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"User"];
    
    
    return path;
}

+(void)reset

{
    
    __strong static NRWCarUser *shareManager;

    [[NSFileManager defaultManager] removeItemAtPath:[NRWCarUser archivePath] error:nil];
    
    shareManager = [NRWCarUser sharedUser];
    
  shareManager.didLogin = NO;
  shareManager.accountId = nil;
  shareManager.accountName = nil;
  shareManager.accountPropertyMap = nil;
  shareManager.applicationId = nil;
  shareManager.marketId = nil;
  shareManager.marketLogo = nil;
  shareManager.marketName = nil;
  shareManager.resourceList = nil;
  shareManager.roleList = nil;
  shareManager.shopId = nil;
  shareManager.shopName = nil;
  shareManager.resourceUrl = nil;
  shareManager.userId = nil;
  shareManager.userMobile = nil;
  shareManager.userName = nil;
   shareManager.password = nil;
   shareManager.phoneNum = nil;

    [shareManager archive];
    
}

@end
